package com.yphone.net.helper;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iiordanov.bVNC.event.bVNCEvent;
import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.base.BaseDataResult;
import com.yphone.net.inf.IAnalysisJson;
import com.yphone.tools.MyLog;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.utils.JsonUtil;
import com.yphone.utils.NetworkUtils;
import com.yphone.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class ActivityHttpHelper<T> extends OkHttpClientHelper {
    /* access modifiers changed from: private */
    public Gson gson;
    private IAnalysisJson mAnalysisJson;
    private IUIDataListener mDataListener = null;
    Handler mHandler = new Handler(Looper.getMainLooper());

    public ActivityHttpHelper(final TypeToken<T> typeToken) {
        if (this.mAnalysisJson == null) {
            this.gson = new Gson();
            this.mAnalysisJson = new IAnalysisJson() {
                public T getData(String json) {
                    try {
                        MyLog.e("接口返回："+json);
                        try {
                            JSONObject jsonObject = new JSONObject(json);
                            int code = jsonObject.optInt("code");
                            if (code == 1001){
                                //登录过期
                                LoginManager.getInstance().clearUserInfo();
                                EventBus.getDefault().post(new bVNCEvent.UInotifyEvent(true));
                            }
                        }catch (JSONException e){
                            return (T) json;
                        }
                        return ActivityHttpHelper.this.gson.fromJson(json, typeToken.getType());
                    } catch (Exception e) {
                        MyLog.e("ActivityHttpHelper", "getData exception " + e.getMessage());
                        e.printStackTrace();
                        return null;
                    }
                }
            };
        }
    }

    public ActivityHttpHelper(IAnalysisJson analysisJson) {
        this.mAnalysisJson = analysisJson;
    }

    public void UpdateUIDataListener(IUIDataListener dataListener) {
        this.mDataListener = dataListener;
    }

    public void onResponse(Object response) {
        if (this.mDataListener != null) {
            this.mDataListener.uiDataSuccess(response);
        } else {
            MyLog.e("ActivityHttpHelper", "onResponse mDataListener==null");
        }
    }

    public void onErrorResponse(Exception e) {
        if (this.mDataListener != null) {
            this.mDataListener.uiDataError(e);
        } else {
            MyLog.e("ActivityHttpHelper", "onErrorResponse mDataListener==null");
        }
    }

    private String getBaseUrl(String url) {
        int head = 0;
        if (url.startsWith("https://")) {
            head = "https://".length();
        } else if (url.startsWith("http://")) {
            head = "http://".length();
        }
        return url.substring(0, url.substring(head).indexOf("/") + head);
    }

    public void sendGetRequest(String url, int timeOut) {
        super.sendGetRequest(getBaseUrl(url), url, this.mAnalysisJson, timeOut);
    }

    public void sendPostRequest(String url, Map<String, String> map, int timeOut, Boolean toast) {
        if (!toast.booleanValue() || NetworkUtils.isConnected()) {
            super.sendPostRequest(getBaseUrl(url), url, map, this.mAnalysisJson, timeOut);
            return;
        }
        ToastUtils.showLong((CharSequence) "无法连接到服务器，请检查您的网络连接");
    }

    public void sendPostRequest(String url, Map<String, String> map, int timeOut) {
        MyLog.e("请求url = "+url);
        for (String key : map.keySet()){
            MyLog.e("参数 : "+key+"  value : "+map.get(key));
        }
        sendPostRequest(getBaseUrl(url), url, map, timeOut);
    }

    public void sendPostRequest(String baseurl, String url, Map<String, String> map, int timeOut) {
        if (!NetworkUtils.isConnected()) {
            this.mHandler.post(new Runnable() {
                public void run() {
                    ToastUtils.showLong((CharSequence) "无法连接到服务器，请检查您的网络连接");
                }
            });
            return;
        }
        super.sendPostRequest(baseurl, url, map, this.mAnalysisJson, timeOut);
    }

    public void uploadResourcePost(String url, String picPath, Map<String, String> map, int timeOut) {
        super.uploadResourcePost(getBaseUrl(url), url, picPath, map, this.mAnalysisJson, timeOut);
    }

    public void stopRequest() {
        if (!isDisposed()) {
            dispose();
        }
        this.mDataListener = null;
        this.gson = null;
    }
}